﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;



class raimuCore 
{
    List<bugData> list = new List<bugData>();
    List<bugData> pluslist = new List<bugData>();
    List<bugData> removeList = new List<bugData>();
    public int down = 0;
    public int add = 0;

    public raimuCore()
    {
        list.Add(new bugData());
    }

    public bool doWork()
    {
        
        foreach (bugData bd in list)
        {
            doTop doNext = bd.Next();

            if (doNext == null)
            {
                bugData bdw = new bugData();
                bugData bde = new bugData();
                bugData bda = new bugData();
                bugData bdk = new bugData();

                bdw.id = bd.id + "W";
                bde.id = bd.id + "E";
                bda.id = bd.id + "A";
                bda.id + bd.id + "K";

                doTop[] temp = bd.getLists();

                bdw.addList(temp);
                bde.addList(temp);
                bda.addList(temp);
                bdk.addList(temp);

                bdw.group = bd.group;
                bde.group = bd.group;
                bda.group = bd.group;
                bdk.group = bd.group;

                if (bdw.group == 0)
                {
                    bdw.group = 1;
                    bde.group = 2;
                    bda.group = 3;
                    bdk.group = 4;
                }
                
                bdw.AddNeuron(bd.getNeuron());
                bde.AddNeuron(bd.getNeuron());
                bda.AddNeuron(bd.getNeuron());
                bdk.AddNeuron(bd.getNeuron());

                bdw.addList(new wait { Owner = this });
                bde.addList(new eat { Owner = this });
                bda.addList(new add { Owner = this });
                bdk.addList(new kill { Owner = this });

                bdw.delList(new None());
                bde.delList(new None());
                bda.delList(new None());
                bdk.delList(new None());

                pluslist.Add(bdw);
                pluslist.Add(bde);
                pluslist.Add(bda);
                pluslist.Add(bdk);

                removeList.Add(bd);
            }
            else
            {
                doNext.Do(bd);
                if (bd.hp <= 0)
                {
                    removeList.Add(bd);
                }
                else
                {
                    bd.AddNeuronSuccess();
                }
            }

        }
        list.AddRange(pluslist);

        down = removeList.Count;
        add = pluslist.Count;
        foreach (bugData rm in removeList)
        {
            Predicate<bugData> pd = new Predicate<bugData>(delegate(bugData bd) { return bd.group == rm.group; });
            Random rnd = new Random();
            if (list.Find(pd) != null)
            {
                bugData[] bd = list.FindAll(pd).ToArray();
                bugData bds = bd[rnd.Next(0,bd.Length)];
                bds.AddNeuron(rm);
            } 
            list.Remove(rm);
        }

        pluslist = new List<bugData>();
        removeList = new List<bugData>();

        return true;
    }

    public void addList(bugData bd)
    {
        pluslist.Add(bd);
    }

    public void DeleteAt(int index)
    {
        removeList.Add(list[index]);
    }

    public int getListCount()
    {
        return list.Count;
    }
    public List<bugData> getList()
    {
        return list;
    }
    public void Delete(bugData bd)
    {
        removeList.Add(bd);
    }
}